package com.eastmelon.blog.service.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.eastmelon.basic.model.Pager;
import com.eastmelon.blog.dao.ChannelDao;
import com.eastmelon.blog.entity.Article;
import com.eastmelon.blog.entity.Channel;
import com.eastmelon.blog.exception.BlogException;
import com.eastmelon.blog.service.ChannelService;
import com.eastmelon.blog.vo.ChannelCount;

@Service("channelService")
public class ChannelServiceImpl implements ChannelService {
	@Resource(name = "channelDao")
	private ChannelDao channelDao;

	@Override
	public Channel get(Serializable id) {
		return channelDao.get(id);
	}

	@Override
	public void save(Channel entity) {
		channelDao.save(entity);

	}

	@Override
	public void update(Channel entity) {
		channelDao.update(entity);
	}

	@Override
	public void deleteById(Serializable id) {
		try {
			channelDao.deleteById(id);
		}
		catch (Exception e) {
			throw new BlogException("该栏目或子栏目还有文章");
		}
	}

	@Override
	public Integer getMaxPPos() {
		return channelDao.maxPPos();
	}

	@Override
	public List<Channel> find() {
		return channelDao.find(null);
	}

	@Override
	public void deleteByIds(Serializable[] ids) {
		try {
			channelDao.deleteByIds(ids);
		}
		catch (Exception e) {
			throw new BlogException("该栏目或子栏目还有文章");
		}
	}

	@Override
	public List<Channel> findParentAndSon() {
		List<Channel> parents = channelDao.findParent();
		for (Channel p : parents) {
			List<Channel> children = channelDao.findByPid(p.getId());
			p.setChildren(children);
		}
		return parents;
	}

	@Override
	public Integer getMaxCPos() {
		return channelDao.maxCPos();
	}

	@Override
	public String getChannelName(Serializable id) {
		return channelDao.getChannelName(id);
	}

	@Override
	public Pager<Channel> findPage() {
		return channelDao.findPage();
	}

	@Override
	public List<Channel> findParent() {
		return channelDao.findParent();
	}

	@Override
	public List<ChannelCount> findChannelCount() {
		return channelDao.findChannelCount();
	}


}
